﻿using Baula.TextGen.ComAndVbeIntegration.ComInterfaces;
using Baula.TextGen.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.TextGen.ComAndVbeIntegration.Tests
{
    [TestClass]
    public class ComSpecialtiesTests
    {
        [TestMethod]
        [ExpectedException(typeof(UnknownPartNameException))]
        public void DocumentTemplateGetPartTemplate_IndexedWithNonExistingPart_ThrowsUnknownPartNameException()
        {
            const string templateContent = @"There is a main part
and the part called ExistingPart
but no part called NonExistingPart
' === ExistingPart";
            var documentTemplate = new DocumentTemplate {Content = templateContent};

            var comDocumentTemplate = documentTemplate as IDocumentTemplateForCom;
#pragma warning disable 168
            var dummy = comDocumentTemplate.GetPartTemplate("NonExistingPart");
#pragma warning restore 168
        }

        [TestMethod]
        public void DocumentAddPart_CalledFirstTime_GivesOnePart()
        {
            const string templateContent = @"Main Part
' === Part1
Part 1";
            IDocumentTemplateForCom documentTemplate = new DocumentTemplate {Content = templateContent};

            IPartTemplateForCom partTemplate = documentTemplate.GetPartTemplate("Part1");
            IPartForCom part = partTemplate.CreatePart();
            IDocumentForCom document = documentTemplate.CreateDocument();

            document.AddPart(part);

            Assert.AreEqual(1, document.PartCount);
        }

        [TestMethod]
        public void PartSetValueForPlaceholder_CalledWithPlaceholder_SetsThatValue()
        {
            const string templateContent = @"' === MyPart
A Part with %APlaceholder%.";
            IDocumentTemplateForCom documentTemplate = new DocumentTemplate { Content = templateContent };

            IPartTemplateForCom myPartTemplate = documentTemplate.GetPartTemplate("MyPart");
            IPartForCom myPart = myPartTemplate.CreatePart();

            const string placeholderName = "APlaceholder";
            const string theValue = "The Value";
            myPart.SetValueForPlaceholder(placeholderName, theValue);

            Assert.AreEqual(theValue, myPart.GetValueForPlaceholders(placeholderName));
        }

        [TestMethod]
        public void DocumentSetValueForPlaceholder_CalledWithPlaceholder_GetsRenderedCorrectly()
        {
            const string templateContent = @"Main Part with %APlaceholder%.";
            var documentTemplate = new DocumentTemplate{Content = templateContent};

            var document = documentTemplate.CreateDocument();
            const string placeholderName = "APlaceholder";
            const string theValue = "The Value";
            ((IDocumentForCom)document).SetValueForPlaceholder(placeholderName, theValue);

            var content = document.RenderToString();
 
            Assert.AreEqual("Main Part with The Value.", content);
        }
    }
}